/**
 * Copyright (c) 2011 Basil Shikin, VintageRadio Project
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.vintagephone.model.player;

import java.io.File;
import java.io.FileFilter;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.vintagephone.model.MusicFolder;
import org.vintagephone.model.Song;

public class PlayerUtils 
{
    private static final Map<String, SoftReference<Song>> s_songCache = new HashMap<String, SoftReference<Song>>();
    
	private static final String s_musicFolderBase = "/sdcard/VintageRadio";
	
	private static final String s_folderOne   = "Jazz";
	private static final String s_folderTwo   = "Piano";
	private static final String s_folderThree = "Misc";
	private static final String s_folderFour  = "News";

	public static List<Song> loadFolderSongs(MusicFolder folder) 
	{
		final File folderFile = resolveFolderFile( folder );
		
		final File[] files = folderFile.listFiles( new FileFilter() {
			public boolean accept(File pathname) {
				return pathname.getName().endsWith(".mp3");
			}
		} );
		
		if ( files != null && files.length > 0 )
		{
			final List<Song> result = new ArrayList<Song>( files.length );
			for ( File file : files )
			{
			    final Song song;
			    
			    final String path = file.getAbsolutePath();
			    final SoftReference<Song> songRef = s_songCache.get( path );
			    
			    if ( songRef != null && songRef.get() != null )
			    {
			        song = songRef.get();
			    }
			    else
			    {
			        song = new Song( file );
			        s_songCache.put( path, new SoftReference<Song>( song ) );
			    }
			    if ( song == null ) throw new RuntimeException("Programming error: no song for \"" + path + "\"");
			    
				result.add( song );
			}
			
			return result;
		}
		else
		{
			return Collections.emptyList();
		}
	}

	public static File resolveFolderFile(MusicFolder folder) 
	{
		switch ( folder) {
		
			case ONE:
				return new File( s_musicFolderBase, s_folderOne );
			case TWO:
				return new File( s_musicFolderBase, s_folderTwo );
			case THREE:
				return new File( s_musicFolderBase, s_folderThree );
			case FOUR:
				return new File( s_musicFolderBase, s_folderFour );

		default:
			throw new IllegalArgumentException("Unknown foldeer: " + folder);
		}
	}

}
